home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4192 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: news.clark.net!usenet
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: Sun, 28 Jan 1996 19:24:05 GMT
  6. Organization: Clark Internet Services, Inc.
  7. Message-ID: <4egiid$j3f@clarknet.clark.net>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <hamilton-1801962045570001@dialup-147.austin.io.com> <4dpcfo$293@clarknet.clark.net> <hamilton-2401960104020001@dialup-86.austin.io.com> <3108c867.40236096@nntp.ix.netcom.com> <4eb6kq$ksf@gazette.tandem.com>
  9. NNTP-Posting-Host: gusty-ppp.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: Forte Free Agent 1.0.82
  14.  
  15. Keep in mind, everyone, that the "hierarchy" of expression evaluation,
  16. including the precedence of expressions inside of parentheses, is a
  17. hierarchy of operators, not of operands.
  18.  
  19. Given a + b * c, the subexpression b * c will be evaluated first and
  20. then the product will be added to a. Given (a + b) * c, the a + b will
  21. be evaluated first, with the sum multiplied by c. This is because *
  22. has higher precedence than + at the same parenthetic level, but in the
  23. second example the + subexpression is inside parentheses and so it is
  24. evaluated first. This is the hierarchy of operators.
  25.  
  26. With regards to an expression like expr1 op expr2, however, the issue
  27. is the order of evaluation of the operands, expr1 and expr2. What
  28. Stroustrup says is "The order of evaluation of subexpressions is
  29. determined by the precedence and grouping of the operators . . . .
  30. Except where noted, the order of evaluation of operands of individual
  31. operators is undefined. In particular, if a value is modified twice in
  32. an expression, the result of the expression is undefined except where
  33. an ordering is guaranteed by the operators involved." Therefore,
  34. except in certain cases, we don't know whether expr1 or expr2 will be
  35. evaluated first. If expr2 modifies i and expr1 uses the value of i,
  36. there is no rule in most cases that tells us whether the value of i
  37. used in expr1 is the value it had before the expression had been
  38. reached or the value to which it had been set by expr2.
  39.  
  40. Operators that guarantee an order of expression are &&, ||, and ?:.
  41. The left operand of logical AND is evaluated first, and if it is 0
  42. (false), the right operand is ignored altogether. If you have an
  43. expression (p && (q = r)), if p is 0, then q won't get assigned the
  44. value of r.
  45.  
  46. The left operand of logical OR is also evaluated first. If the left
  47. operand evaluates to nonzero (true), then the right operand is not
  48. evaluated.
  49.  
  50.  
  51.  
  52.  
  53.